'use strict';
const http = require('../utils/httpUtils');
const namespace = require('./namespace');
/* global getCookie */
module.exports = function (app) {
  //店铺列表
  app.get(`${namespace.root}/shop/index`, async function (req, res) {

    res.render('shop/index', {

    });
  });

  //推荐店铺列表
  app.get(`${namespace.root}/shop/recommend_shop`, async function (req, res) {

    res.render('shop/recommend_shop', {

    });
  });

  //店铺详情
  app.get(`${namespace.root}/shop/detail`, async function (req, res) {
    let result = await http.get(`/api/mallShop/web/query`, {
      id: req.query.id
    }, '/antique').catch(e => {
      res.send({
        errCode: 401
      });
    });
    res.render('shop/detail', {
      id: req.query.id,
      shopDetail: result.data,
      isYijia: req.query.isYijia
    });
  });

  //添加店铺
  app.get(`${namespace.root}/shop/add`, async function (req, res) {
    res.render('shop/add', {
      shopDetail: ''
    });
  });

  //修改店铺
  app.get(`${namespace.root}/shop/update`, async function (req, res){
    let result = await http.get(`/api/mallShop/manage/query_myshop`, {
      token: req.cookies.token
    }, '/antique').catch(e => {
      res.send({
        errCode: 401
      });
    });
    console.log(result.data);
    res.render('shop/add', {
      shopDetail: JSON.stringify(result.data)
    });
  });

  //添加专场
  app.get(`${namespace.root}/addScene`, async function (req, res){

    res.render('addScene', {
      dataDetail: ''
    });
  });

  //修改专场
  app.get(`${namespace.root}/updateScene`, async function (req, res) {
    let result = {};
    let resultData = {};
    if (req.query && req.query.id) {
      result = await http.get(`/api/mallAuction/manage/query`, {
        token: req.cookies.token,
        id: req.query.id
      }, '/antique').catch(e => {
        res.send({
          errCode: 401
        });
      });

    }
    if (result && result.data) {
      resultData = result.data;
      var date = new Date(resultData.endDate);
      var Y = date.getFullYear() + '-';
      var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
      var D = date.getDate() + ' ';
      var h = date.getHours() + ':';
      var m = date.getMinutes();
      var s = date.getSeconds();
      var time = Y + M + D + h + m;
      resultData.endDate = time;
    }

    result = JSON.stringify(resultData);
    res.render('addScene', {
      dataDetail: result
    });

  });

  //我的粉丝
  app.get(`${namespace.root}/shop/followUser`, async function (req, res) {

    res.render('shop/followUser', {
      shopId: req.query.shopId
    });
  });

  //店铺报表
  app.get(`${namespace.root}/shop/report`, async function (req, res) {
    //取得资金报表信息
    var capitalReport = await http.get(`/api/report/capital_report`, {
      token: req.cookies.token
    }, '/antique').catch(e => {
      res.send({
        errCode: 401
      });
    });
    
    //取得经营报表信息
    var managementReport = await http.get(`/api/report/operation_report`, {
      token: req.cookies.token
    }, '/antique').catch(e => {
      res.send({
        errCode: 401
      });
    });

    res.render('shop/report', {
      shopId: req.query.shopId,
      capitalReport:capitalReport.data,
      managementReport:managementReport.data
    });
  });
};